{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Classification skill"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>text</th>\n",
       "      <th>category</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>Apple product with a sleek design.</td>\n",
       "      <td>Electronics</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>Laptop stand for the kitchen.</td>\n",
       "      <td>Furniture/Home Decor</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>Chocolate leather boots.</td>\n",
       "      <td>Footwear/Clothing</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>Wooden cream for surfaces.</td>\n",
       "      <td>Furniture/Home Decor</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>Natural finish for your lips.</td>\n",
       "      <td>Beauty/Personal Care</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                                 text              category\n",
       "0  Apple product with a sleek design.           Electronics\n",
       "1       Laptop stand for the kitchen.  Furniture/Home Decor\n",
       "2            Chocolate leather boots.     Footwear/Clothing\n",
       "3          Wooden cream for surfaces.  Furniture/Home Decor\n",
       "4       Natural finish for your lips.  Beauty/Personal Care"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import pandas as pd\n",
    "df = pd.DataFrame([\n",
    "    {\"text\": \"Apple product with a sleek design.\", \"category\": \"Electronics\"},\n",
    "    {\"text\": \"Laptop stand for the kitchen.\", \"category\": \"Furniture/Home Decor\"},\n",
    "    {\"text\": \"Chocolate leather boots.\", \"category\": \"Footwear/Clothing\"},\n",
    "    {\"text\": \"Wooden cream for surfaces.\", \"category\": \"Furniture/Home Decor\"},\n",
    "    {\"text\": \"Natural finish for your lips.\", \"category\": \"Beauty/Personal Care\"}\n",
    "])\n",
    "df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<pre style=\"white-space:pre;overflow-x:auto;line-height:normal;font-family:Menlo,'DejaVu Sans Mono',consolas,'Courier New',monospace\">\n",
       "\n",
       "=&gt; Iteration #<span style=\"color: #008080; text-decoration-color: #008080; font-weight: bold\">0</span>: Getting feedback, analyzing and improving <span style=\"color: #808000; text-decoration-color: #808000\">...</span>\n",
       "</pre>\n"
      ],
      "text/plain": [
       "\n",
       "\n",
       "=> Iteration #\u001b[1;36m0\u001b[0m: Getting feedback, analyzing and improving \u001b[33m...\u001b[0m\n"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/html": [
       "<pre style=\"white-space:pre;overflow-x:auto;line-height:normal;font-family:Menlo,'DejaVu Sans Mono',consolas,'Courier New',monospace\">Applying skill: product_category_classification\n",
       "</pre>\n"
      ],
      "text/plain": [
       "Applying skill: product_category_classification\n"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "100%|█| 5/5 [00:00<00:00, \n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<pre style=\"white-space:pre;overflow-x:auto;line-height:normal;font-family:Menlo,'DejaVu Sans Mono',consolas,'Courier New',monospace\">Predictions and feedback:\n",
       "</pre>\n"
      ],
      "text/plain": [
       "Predictions and feedback:\n"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/html": [
       "<pre style=\"white-space:pre;overflow-x:auto;line-height:normal;font-family:Menlo,'DejaVu Sans Mono',consolas,'Courier New',monospace\">                                                                                                                   \n",
       " <span style=\"color: #800080; text-decoration-color: #800080; font-weight: bold\"> predicted_category__fb          </span> <span style=\"color: #800080; text-decoration-color: #800080; font-weight: bold\"> text                            </span> <span style=\"color: #800080; text-decoration-color: #800080; font-weight: bold\"> category             </span> <span style=\"color: #800080; text-decoration-color: #800080; font-weight: bold\"> predicted_category   </span> \n",
       " ───────────────────────────────────────────────────────────────────────────────────────────────────────────────── \n",
       "  Prediction is correct.            Apple product with a sleek        Electronics            Electronics           \n",
       "                                    design.                                                                        \n",
       " <span style=\"color: #7f7f7f; text-decoration-color: #7f7f7f\"> Prediction is incorrect.        </span> <span style=\"color: #7f7f7f; text-decoration-color: #7f7f7f\"> Laptop stand for the kitchen.   </span> <span style=\"color: #7f7f7f; text-decoration-color: #7f7f7f\"> Furniture/Home Decor </span> <span style=\"color: #7f7f7f; text-decoration-color: #7f7f7f\"> Electronics          </span> \n",
       " <span style=\"color: #7f7f7f; text-decoration-color: #7f7f7f\"> Correct answer: \"Furniture/Home </span> <span style=\"color: #7f7f7f; text-decoration-color: #7f7f7f\">                                 </span> <span style=\"color: #7f7f7f; text-decoration-color: #7f7f7f\">                      </span> <span style=\"color: #7f7f7f; text-decoration-color: #7f7f7f\">                      </span> \n",
       " <span style=\"color: #7f7f7f; text-decoration-color: #7f7f7f\"> Decor\"                          </span> <span style=\"color: #7f7f7f; text-decoration-color: #7f7f7f\">                                 </span> <span style=\"color: #7f7f7f; text-decoration-color: #7f7f7f\">                      </span> <span style=\"color: #7f7f7f; text-decoration-color: #7f7f7f\">                      </span> \n",
       "  Prediction is correct.            Chocolate leather boots.          Footwear/Clothing      Footwear/Clothing     \n",
       " <span style=\"color: #7f7f7f; text-decoration-color: #7f7f7f\"> Prediction is correct.          </span> <span style=\"color: #7f7f7f; text-decoration-color: #7f7f7f\"> Wooden cream for surfaces.      </span> <span style=\"color: #7f7f7f; text-decoration-color: #7f7f7f\"> Furniture/Home Decor </span> <span style=\"color: #7f7f7f; text-decoration-color: #7f7f7f\"> Furniture/Home Decor </span> \n",
       "  Prediction is correct.            Natural finish for your lips.     Beauty/Personal Care   Beauty/Personal Care  \n",
       "                                                                                                                   \n",
       "</pre>\n"
      ],
      "text/plain": [
       "                                                                                                                   \n",
       " \u001b[1;35m \u001b[0m\u001b[1;35mpredicted_category__fb         \u001b[0m\u001b[1;35m \u001b[0m \u001b[1;35m \u001b[0m\u001b[1;35mtext                           \u001b[0m\u001b[1;35m \u001b[0m \u001b[1;35m \u001b[0m\u001b[1;35mcategory            \u001b[0m\u001b[1;35m \u001b[0m \u001b[1;35m \u001b[0m\u001b[1;35mpredicted_category  \u001b[0m\u001b[1;35m \u001b[0m \n",
       " ───────────────────────────────────────────────────────────────────────────────────────────────────────────────── \n",
       "  Prediction is correct.            Apple product with a sleek        Electronics            Electronics           \n",
       "                                    design.                                                                        \n",
       " \u001b[2m \u001b[0m\u001b[2mPrediction is incorrect.       \u001b[0m\u001b[2m \u001b[0m \u001b[2m \u001b[0m\u001b[2mLaptop stand for the kitchen.  \u001b[0m\u001b[2m \u001b[0m \u001b[2m \u001b[0m\u001b[2mFurniture/Home Decor\u001b[0m\u001b[2m \u001b[0m \u001b[2m \u001b[0m\u001b[2mElectronics         \u001b[0m\u001b[2m \u001b[0m \n",
       " \u001b[2m \u001b[0m\u001b[2mCorrect answer: \"Furniture/Home\u001b[0m\u001b[2m \u001b[0m \u001b[2m                                 \u001b[0m \u001b[2m                      \u001b[0m \u001b[2m                      \u001b[0m \n",
       " \u001b[2m \u001b[0m\u001b[2mDecor\"                         \u001b[0m\u001b[2m \u001b[0m \u001b[2m                                 \u001b[0m \u001b[2m                      \u001b[0m \u001b[2m                      \u001b[0m \n",
       "  Prediction is correct.            Chocolate leather boots.          Footwear/Clothing      Footwear/Clothing     \n",
       " \u001b[2m \u001b[0m\u001b[2mPrediction is correct.         \u001b[0m\u001b[2m \u001b[0m \u001b[2m \u001b[0m\u001b[2mWooden cream for surfaces.     \u001b[0m\u001b[2m \u001b[0m \u001b[2m \u001b[0m\u001b[2mFurniture/Home Decor\u001b[0m\u001b[2m \u001b[0m \u001b[2m \u001b[0m\u001b[2mFurniture/Home Decor\u001b[0m\u001b[2m \u001b[0m \n",
       "  Prediction is correct.            Natural finish for your lips.     Beauty/Personal Care   Beauty/Personal Care  \n",
       "                                                                                                                   \n"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/html": [
       "<pre style=\"white-space:pre;overflow-x:auto;line-height:normal;font-family:Menlo,'DejaVu Sans Mono',consolas,'Courier New',monospace\"><span style=\"color: #800000; text-decoration-color: #800000; font-weight: bold\">Skill output to improve: </span><span style=\"color: #008000; text-decoration-color: #008000\">\"predicted_category\"</span><span style=\"color: #800000; text-decoration-color: #800000; font-weight: bold\"> (</span><span style=\"color: #808000; text-decoration-color: #808000; font-weight: bold\">Skill</span><span style=\"color: #800000; text-decoration-color: #800000; font-weight: bold\">=</span><span style=\"color: #008000; text-decoration-color: #008000\">\"product_category_classification\"</span><span style=\"color: #800000; text-decoration-color: #800000; font-weight: bold\">)</span>\n",
       "<span style=\"color: #800000; text-decoration-color: #800000; font-weight: bold\">Accuracy = </span><span style=\"color: #008080; text-decoration-color: #008080; font-weight: bold\">80.00</span><span style=\"color: #800000; text-decoration-color: #800000; font-weight: bold\">%</span>\n",
       "</pre>\n"
      ],
      "text/plain": [
       "\u001b[1;31mSkill output to improve: \u001b[0m\u001b[32m\"predicted_category\"\u001b[0m\u001b[1;31m \u001b[0m\u001b[1;31m(\u001b[0m\u001b[1;33mSkill\u001b[0m\u001b[1;31m=\u001b[0m\u001b[32m\"product_category_classification\"\u001b[0m\u001b[1;31m)\u001b[0m\n",
       "\u001b[1;31mAccuracy = \u001b[0m\u001b[1;36m80.00\u001b[0m\u001b[1;31m%\u001b[0m\n"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\"{system}\":\n",
      "You are a helpful assistant.\n",
      "\"{user}\":\n",
      "\n",
      "A prompt is a text paragraph that outlines the expected actions and instructs the large language model (LLM) to generate a specific output. This prompt is concatenated with the input text, and the model then creates the required output.\n",
      "This describes the full template how the prompt is concatenated with the input to produce the output:\n",
      "\n",
      "```\n",
      "\n",
      "{prompt}\n",
      "Text: {text}\n",
      "Category: {predicted_category}\n",
      "```\n",
      "\n",
      "Here:\n",
      "- \"Text: {text}\" is input template,\n",
      "- \"{prompt}\" is the LLM prompt,\n",
      "- \"Category: {predicted_category}\" is the output template.\n",
      "\n",
      "Model can produce erroneous output if a prompt is not well defined. In our collaboration, we’ll work together to refine a prompt. The process consists of two main steps:\n",
      "\n",
      "## Step 1\n",
      "I will provide you with the current prompt along with prediction examples. Each example contains the input text, the final prediction produced by the model, and the user feedback. User feedback indicates whether the model prediction is correct or not. Your task is to analyze the examples and user feedback, determining whether the existing instruction is describing the task reflected by these examples precisely, and suggests changes to the prompt to address the incorrect predictions.\n",
      "\n",
      "## Step 2\n",
      "Next, you will carefully review your reasoning in step 1, integrate the insights to refine the prompt, and provide me with the new prompt that improves the model’s performance.\n",
      "\"{assistant}\":\n",
      "Sure, I’d be happy to help you with this prompt engineering problem. Please provide me with the current prompt and the examples with user feedback.\n",
      "\"{user}\":\n",
      "\n",
      "## Current prompt\n",
      "Label the input text with the following labels: {labels}\n",
      "\n",
      "## Examples\n",
      "### Example #0\n",
      "\n",
      "Text: Apple product with a sleek design.\n",
      "\n",
      "Category: Electronics\n",
      "\n",
      "User feedback: Prediction is correct.\n",
      "\n",
      "\n",
      "### Example #1\n",
      "\n",
      "Text: Laptop stand for the kitchen.\n",
      "\n",
      "Category: Electronics\n",
      "\n",
      "User feedback: Prediction is incorrect. Correct answer: \"Furniture/Home Decor\"\n",
      "\n",
      "\n",
      "### Example #2\n",
      "\n",
      "Text: Chocolate leather boots.\n",
      "\n",
      "Category: Footwear/Clothing\n",
      "\n",
      "User feedback: Prediction is correct.\n",
      "\n",
      "\n",
      "### Example #3\n",
      "\n",
      "Text: Wooden cream for surfaces.\n",
      "\n",
      "Category: Furniture/Home Decor\n",
      "\n",
      "User feedback: Prediction is correct.\n",
      "\n",
      "\n",
      "### Example #4\n",
      "\n",
      "Text: Natural finish for your lips.\n",
      "\n",
      "Category: Beauty/Personal Care\n",
      "\n",
      "User feedback: Prediction is correct.\n",
      "\n",
      "\n",
      "\n",
      "Summarize your analysis about incorrect predictions and suggest changes to the prompt.\n",
      "\"{assistant}\":\n",
      "Based on the examples and user feedback, it seems that the current prompt is not specific enough to guide the model to accurately predict the categories. \n",
      "\n",
      "In example #1, the model wrongly predicted \"Electronics\" instead of \"Furniture/Home Decor\" for the text \"Laptop stand for the kitchen.\" This suggests that the prompt should provide more context about the expected category, such as whether it belongs to electronics, furniture, clothing, etc.\n",
      "\n",
      "To address this, I suggest the following changes to the prompt:\n",
      "\n",
      "## Updated Prompt\n",
      "Categorize the input text into the most appropriate category from the following options: {categories}.\n",
      "\n",
      "With this updated prompt, we explicitly ask the model to categorize the text into the most appropriate category from a predefined list of categories. This provides clearer instructions to the model and helps improve its accuracy in predicting the categories.\n",
      "\n",
      "Please let me know if you have any further suggestions or if there's anything else I can assist you with.\n",
      "\"{user}\":\n",
      "\n",
      "Now please carefully review your reasoning in Step 1 and help with Step 2: refining the prompt.\n",
      "\n",
      "## Current prompt\n",
      "Label the input text with the following labels: {labels}\n",
      "\n",
      "## Follow this guidance to refine the prompt:\n",
      "\n",
      "1. The new prompt should should describe the task precisely, and address the points raised in the user feedback.\n",
      "\n",
      "2. The new prompt should be similar to the current instruction, and only differ in the parts that address the issues you identified in Step 1.\n",
      "    Example:\n",
      "    - Current prompt: \"The model should generate a summary of the input text.\"\n",
      "    - New prompt: \"The model should generate a summary of the input text. Pay attention to the original style.\"\n",
      "\n",
      "3. Reply only with the new prompt. Do not include input and output templates in the prompt.\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "Label the input text with the <span style=\"background-color: lightgreen;\">a</span><span style=\"background-color: lightgreen;\">p</span><span style=\"background-color: lightgreen;\">p</span><span style=\"background-color: lightgreen;\">r</span><span style=\"background-color: lightgreen;\">o</span><span style=\"background-color: lightgreen;\">p</span><span style=\"background-color: lightgreen;\">r</span><span style=\"background-color: lightgreen;\">i</span><span style=\"background-color: lightgreen;\">a</span><span style=\"background-color: lightgreen;\">t</span><span style=\"background-color: lightgreen;\">e</span><span style=\"background-color: lightgreen;\"> </span><span style=\"background-color: lightgreen;\">c</span><span style=\"background-color: lightgreen;\">a</span><span style=\"background-color: lightgreen;\">t</span><span style=\"background-color: lightgreen;\">e</span><span style=\"background-color: lightgreen;\">g</span><span style=\"background-color: lightgreen;\">o</span><span style=\"background-color: lightgreen;\">r</span><span style=\"background-color: lightgreen;\">y</span><span style=\"background-color: lightgreen;\"> </span><span style=\"background-color: lightgreen;\">f</span><span style=\"background-color: lightgreen;\">r</span><span style=\"background-color: lightgreen;\">o</span><span style=\"background-color: lightgreen;\">m</span><span style=\"background-color: lightgreen;\"> </span><span style=\"background-color: lightgreen;\">t</span><span style=\"background-color: lightgreen;\">h</span><span style=\"background-color: lightgreen;\">e</span><span style=\"background-color: lightgreen;\"> </span>following <span style=\"background-color: lightgreen;\">o</span><span style=\"background-color: lightgreen;\">p</span><span style=\"background-color: lightgreen;\">t</span><span style=\"background-color: lightgreen;\">i</span><span style=\"background-color: lightgreen;\">o</span><span style=\"background-color: lightgreen;\">n</span><span style=\"background-color: lightgreen;\">s</span><span style=\"background-color: lightgreen;\">:</span><span style=\"background-color: lightgreen;\"> </span><span style=\"background-color: lightgreen;\">{</span><span style=\"background-color: lightgreen;\">c</span>a<span style=\"background-color: lightgreen;\">t</span><span style=\"background-color: lightgreen;\">e</span><span style=\"background-color: lightgreen;\">g</span><span style=\"background-color: lightgreen;\">o</span><span style=\"background-color: lightgreen;\">r</span><span style=\"background-color: lightgreen;\">i</span><span style=\"background-color: lightgreen;\">e</span><span style=\"background-color: lightgreen;\">s</span><span style=\"background-color: lightgreen;\">}</span><span style=\"background-color: lightgreen;\">.</span><span style=\"background-color: lightgreen;\"> </span><span style=\"background-color: lightgreen;\">E</span><span style=\"background-color: lightgreen;\">n</span><span style=\"background-color: lightgreen;\">s</span><span style=\"background-color: lightgreen;\">u</span><span style=\"background-color: lightgreen;\">r</span><span style=\"background-color: lightgreen;\">e</span><span style=\"background-color: lightgreen;\"> </span><span style=\"background-color: lightgreen;\">t</span><span style=\"background-color: lightgreen;\">h</span><span style=\"background-color: lightgreen;\">a</span><span style=\"background-color: lightgreen;\">t</span><span style=\"background-color: lightgreen;\"> </span><span style=\"background-color: lightgreen;\">t</span><span style=\"background-color: lightgreen;\">h</span><span style=\"background-color: lightgreen;\">e</span><span style=\"background-color: lightgreen;\"> </span><span style=\"background-color: lightgreen;\">p</span><span style=\"background-color: lightgreen;\">r</span><span style=\"background-color: lightgreen;\">e</span><span style=\"background-color: lightgreen;\">d</span><span style=\"background-color: lightgreen;\">i</span><span style=\"background-color: lightgreen;\">c</span><span style=\"background-color: lightgreen;\">t</span><span style=\"background-color: lightgreen;\">e</span><span style=\"background-color: lightgreen;\">d</span><span style=\"background-color: lightgreen;\"> </span><span style=\"background-color: lightgreen;\">c</span><span style=\"background-color: lightgreen;\">a</span><span style=\"background-color: lightgreen;\">t</span><span style=\"background-color: lightgreen;\">e</span><span style=\"background-color: lightgreen;\">g</span><span style=\"background-color: lightgreen;\">o</span><span style=\"background-color: lightgreen;\">r</span><span style=\"background-color: lightgreen;\">y</span><span style=\"background-color: lightgreen;\"> </span><span style=\"background-color: lightgreen;\">a</span><span style=\"background-color: lightgreen;\">c</span><span style=\"background-color: lightgreen;\">c</span><span style=\"background-color: lightgreen;\">u</span><span style=\"background-color: lightgreen;\">r</span><span style=\"background-color: lightgreen;\">a</span><span style=\"background-color: lightgreen;\">t</span><span style=\"background-color: lightgreen;\">e</span><span style=\"background-color: lightgreen;\">l</span><span style=\"background-color: lightgreen;\">y</span><span style=\"background-color: lightgreen;\"> </span><span style=\"background-color: lightgreen;\">r</span><span style=\"background-color: lightgreen;\">e</span><span style=\"background-color: lightgreen;\">f</span><span style=\"background-color: lightgreen;\">l</span><span style=\"background-color: lightgreen;\">e</span><span style=\"background-color: lightgreen;\">c</span><span style=\"background-color: lightgreen;\">t</span><span style=\"background-color: lightgreen;\">s</span><span style=\"background-color: lightgreen;\"> </span><span style=\"background-color: lightgreen;\">t</span><span style=\"background-color: lightgreen;\">h</span><span style=\"background-color: lightgreen;\">e</span><span style=\"background-color: lightgreen;\"> </span><span style=\"background-color: lightgreen;\">c</span><span style=\"background-color: lightgreen;\">o</span><span style=\"background-color: lightgreen;\">n</span><span style=\"background-color: lightgreen;\">t</span><span style=\"background-color: lightgreen;\">e</span><span style=\"background-color: lightgreen;\">n</span><span style=\"background-color: lightgreen;\">t</span><span style=\"background-color: lightgreen;\"> </span><span style=\"background-color: lightgreen;\">o</span><span style=\"background-color: lightgreen;\">f</span><span style=\"background-color: lightgreen;\"> </span><span style=\"background-color: lightgreen;\">t</span><span style=\"background-color: lightgreen;\">h</span><span style=\"background-color: lightgreen;\">e</span><span style=\"background-color: lightgreen;\"> </span><span style=\"background-color: lightgreen;\">i</span><span style=\"background-color: lightgreen;\">n</span><span style=\"background-color: lightgreen;\">p</span><span style=\"background-color: lightgreen;\">u</span><span style=\"background-color: lightgreen;\">t</span><span style=\"background-color: lightgreen;\"> </span><span style=\"background-color: lightgreen;\">t</span><span style=\"background-color: lightgreen;\">e</span><span style=\"background-color: lightgreen;\">x</span><span style=\"background-color: lightgreen;\">t</span><span style=\"background-color: lightgreen;\">,</span><span style=\"background-color: lightgreen;\"> </span><span style=\"background-color: lightgreen;\">a</span><span style=\"background-color: lightgreen;\">s</span><span style=\"background-color: lightgreen;\"> </span><span style=\"background-color: lightgreen;\">i</span><span style=\"background-color: lightgreen;\">n</span><span style=\"background-color: lightgreen;\">d</span><span style=\"background-color: lightgreen;\">i</span><span style=\"background-color: lightgreen;\">c</span><span style=\"background-color: lightgreen;\">a</span><span style=\"background-color: lightgreen;\">t</span><span style=\"background-color: lightgreen;\">e</span><span style=\"background-color: lightgreen;\">d</span><span style=\"background-color: lightgreen;\"> </span>b<span style=\"background-color: lightgreen;\">y</span> <span style=\"background-color: lightgreen;\">t</span><span style=\"background-color: lightgreen;\">h</span><span style=\"background-color: lightgreen;\">e</span><span style=\"background-color: lightgreen;\"> </span><span style=\"background-color: lightgreen;\">u</span><span style=\"background-color: lightgreen;\">s</span><span style=\"background-color: lightgreen;\">e</span><span style=\"background-color: lightgreen;\">r</span><span style=\"background-color: lightgreen;\"> </span><span style=\"background-color: lightgreen;\">f</span><span style=\"background-color: lightgreen;\">e</span><span style=\"background-color: lightgreen;\">e</span><span style=\"background-color: lightgreen;\">d</span>b<span style=\"background-color: lightgreen;\">a</span><span style=\"background-color: lightgreen;\">c</span><span style=\"background-color: lightgreen;\">k</span><span style=\"background-color: lightgreen;\">.</span>"
      ],
      "text/plain": [
       "<IPython.core.display.HTML object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/html": [
       "<pre style=\"white-space:pre;overflow-x:auto;line-height:normal;font-family:Menlo,'DejaVu Sans Mono',consolas,'Courier New',monospace\">\n",
       "\n",
       "=&gt; Iteration #<span style=\"color: #008080; text-decoration-color: #008080; font-weight: bold\">1</span>: Getting feedback, analyzing and improving <span style=\"color: #808000; text-decoration-color: #808000\">...</span>\n",
       "</pre>\n"
      ],
      "text/plain": [
       "\n",
       "\n",
       "=> Iteration #\u001b[1;36m1\u001b[0m: Getting feedback, analyzing and improving \u001b[33m...\u001b[0m\n"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/html": [
       "<pre style=\"white-space:pre;overflow-x:auto;line-height:normal;font-family:Menlo,'DejaVu Sans Mono',consolas,'Courier New',monospace\">Applying skill: product_category_classification\n",
       "</pre>\n"
      ],
      "text/plain": [
       "Applying skill: product_category_classification\n"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "100%|█| 5/5 [00:01<00:00, \n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<pre style=\"white-space:pre;overflow-x:auto;line-height:normal;font-family:Menlo,'DejaVu Sans Mono',consolas,'Courier New',monospace\">Predictions and feedback:\n",
       "</pre>\n"
      ],
      "text/plain": [
       "Predictions and feedback:\n"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/html": [
       "<pre style=\"white-space:pre;overflow-x:auto;line-height:normal;font-family:Menlo,'DejaVu Sans Mono',consolas,'Courier New',monospace\">                                                                                                             \n",
       " <span style=\"color: #800080; text-decoration-color: #800080; font-weight: bold\"> predicted_category__fb </span> <span style=\"color: #800080; text-decoration-color: #800080; font-weight: bold\"> text                               </span> <span style=\"color: #800080; text-decoration-color: #800080; font-weight: bold\"> category             </span> <span style=\"color: #800080; text-decoration-color: #800080; font-weight: bold\"> predicted_category   </span> \n",
       " ─────────────────────────────────────────────────────────────────────────────────────────────────────────── \n",
       "  Prediction is correct.   Apple product with a sleek design.   Electronics            Electronics           \n",
       " <span style=\"color: #7f7f7f; text-decoration-color: #7f7f7f\"> Prediction is correct. </span> <span style=\"color: #7f7f7f; text-decoration-color: #7f7f7f\"> Laptop stand for the kitchen.      </span> <span style=\"color: #7f7f7f; text-decoration-color: #7f7f7f\"> Furniture/Home Decor </span> <span style=\"color: #7f7f7f; text-decoration-color: #7f7f7f\"> Furniture/Home Decor </span> \n",
       "  Prediction is correct.   Chocolate leather boots.             Footwear/Clothing      Footwear/Clothing     \n",
       " <span style=\"color: #7f7f7f; text-decoration-color: #7f7f7f\"> Prediction is correct. </span> <span style=\"color: #7f7f7f; text-decoration-color: #7f7f7f\"> Wooden cream for surfaces.         </span> <span style=\"color: #7f7f7f; text-decoration-color: #7f7f7f\"> Furniture/Home Decor </span> <span style=\"color: #7f7f7f; text-decoration-color: #7f7f7f\"> Furniture/Home Decor </span> \n",
       "  Prediction is correct.   Natural finish for your lips.        Beauty/Personal Care   Beauty/Personal Care  \n",
       "                                                                                                             \n",
       "</pre>\n"
      ],
      "text/plain": [
       "                                                                                                             \n",
       " \u001b[1;35m \u001b[0m\u001b[1;35mpredicted_category__fb\u001b[0m\u001b[1;35m \u001b[0m \u001b[1;35m \u001b[0m\u001b[1;35mtext                              \u001b[0m\u001b[1;35m \u001b[0m \u001b[1;35m \u001b[0m\u001b[1;35mcategory            \u001b[0m\u001b[1;35m \u001b[0m \u001b[1;35m \u001b[0m\u001b[1;35mpredicted_category  \u001b[0m\u001b[1;35m \u001b[0m \n",
       " ─────────────────────────────────────────────────────────────────────────────────────────────────────────── \n",
       "  Prediction is correct.   Apple product with a sleek design.   Electronics            Electronics           \n",
       " \u001b[2m \u001b[0m\u001b[2mPrediction is correct.\u001b[0m\u001b[2m \u001b[0m \u001b[2m \u001b[0m\u001b[2mLaptop stand for the kitchen.     \u001b[0m\u001b[2m \u001b[0m \u001b[2m \u001b[0m\u001b[2mFurniture/Home Decor\u001b[0m\u001b[2m \u001b[0m \u001b[2m \u001b[0m\u001b[2mFurniture/Home Decor\u001b[0m\u001b[2m \u001b[0m \n",
       "  Prediction is correct.   Chocolate leather boots.             Footwear/Clothing      Footwear/Clothing     \n",
       " \u001b[2m \u001b[0m\u001b[2mPrediction is correct.\u001b[0m\u001b[2m \u001b[0m \u001b[2m \u001b[0m\u001b[2mWooden cream for surfaces.        \u001b[0m\u001b[2m \u001b[0m \u001b[2m \u001b[0m\u001b[2mFurniture/Home Decor\u001b[0m\u001b[2m \u001b[0m \u001b[2m \u001b[0m\u001b[2mFurniture/Home Decor\u001b[0m\u001b[2m \u001b[0m \n",
       "  Prediction is correct.   Natural finish for your lips.        Beauty/Personal Care   Beauty/Personal Care  \n",
       "                                                                                                             \n"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/html": [
       "<pre style=\"white-space:pre;overflow-x:auto;line-height:normal;font-family:Menlo,'DejaVu Sans Mono',consolas,'Courier New',monospace\">No errors found!\n",
       "</pre>\n"
      ],
      "text/plain": [
       "No errors found!\n"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/html": [
       "<pre style=\"white-space:pre;overflow-x:auto;line-height:normal;font-family:Menlo,'DejaVu Sans Mono',consolas,'Courier New',monospace\">\n",
       "\n",
       "=&gt; Iteration #<span style=\"color: #008080; text-decoration-color: #008080; font-weight: bold\">2</span>: Getting feedback, analyzing and improving <span style=\"color: #808000; text-decoration-color: #808000\">...</span>\n",
       "</pre>\n"
      ],
      "text/plain": [
       "\n",
       "\n",
       "=> Iteration #\u001b[1;36m2\u001b[0m: Getting feedback, analyzing and improving \u001b[33m...\u001b[0m\n"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/html": [
       "<pre style=\"white-space:pre;overflow-x:auto;line-height:normal;font-family:Menlo,'DejaVu Sans Mono',consolas,'Courier New',monospace\">Applying skill: product_category_classification\n",
       "</pre>\n"
      ],
      "text/plain": [
       "Applying skill: product_category_classification\n"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "100%|█| 5/5 [00:00<00:00, \n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<pre style=\"white-space:pre;overflow-x:auto;line-height:normal;font-family:Menlo,'DejaVu Sans Mono',consolas,'Courier New',monospace\">Predictions and feedback:\n",
       "</pre>\n"
      ],
      "text/plain": [
       "Predictions and feedback:\n"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/html": [
       "<pre style=\"white-space:pre;overflow-x:auto;line-height:normal;font-family:Menlo,'DejaVu Sans Mono',consolas,'Courier New',monospace\">                                                                                                             \n",
       " <span style=\"color: #800080; text-decoration-color: #800080; font-weight: bold\"> predicted_category__fb </span> <span style=\"color: #800080; text-decoration-color: #800080; font-weight: bold\"> text                               </span> <span style=\"color: #800080; text-decoration-color: #800080; font-weight: bold\"> category             </span> <span style=\"color: #800080; text-decoration-color: #800080; font-weight: bold\"> predicted_category   </span> \n",
       " ─────────────────────────────────────────────────────────────────────────────────────────────────────────── \n",
       "  Prediction is correct.   Apple product with a sleek design.   Electronics            Electronics           \n",
       " <span style=\"color: #7f7f7f; text-decoration-color: #7f7f7f\"> Prediction is correct. </span> <span style=\"color: #7f7f7f; text-decoration-color: #7f7f7f\"> Laptop stand for the kitchen.      </span> <span style=\"color: #7f7f7f; text-decoration-color: #7f7f7f\"> Furniture/Home Decor </span> <span style=\"color: #7f7f7f; text-decoration-color: #7f7f7f\"> Furniture/Home Decor </span> \n",
       "  Prediction is correct.   Chocolate leather boots.             Footwear/Clothing      Footwear/Clothing     \n",
       " <span style=\"color: #7f7f7f; text-decoration-color: #7f7f7f\"> Prediction is correct. </span> <span style=\"color: #7f7f7f; text-decoration-color: #7f7f7f\"> Wooden cream for surfaces.         </span> <span style=\"color: #7f7f7f; text-decoration-color: #7f7f7f\"> Furniture/Home Decor </span> <span style=\"color: #7f7f7f; text-decoration-color: #7f7f7f\"> Furniture/Home Decor </span> \n",
       "  Prediction is correct.   Natural finish for your lips.        Beauty/Personal Care   Beauty/Personal Care  \n",
       "                                                                                                             \n",
       "</pre>\n"
      ],
      "text/plain": [
       "                                                                                                             \n",
       " \u001b[1;35m \u001b[0m\u001b[1;35mpredicted_category__fb\u001b[0m\u001b[1;35m \u001b[0m \u001b[1;35m \u001b[0m\u001b[1;35mtext                              \u001b[0m\u001b[1;35m \u001b[0m \u001b[1;35m \u001b[0m\u001b[1;35mcategory            \u001b[0m\u001b[1;35m \u001b[0m \u001b[1;35m \u001b[0m\u001b[1;35mpredicted_category  \u001b[0m\u001b[1;35m \u001b[0m \n",
       " ─────────────────────────────────────────────────────────────────────────────────────────────────────────── \n",
       "  Prediction is correct.   Apple product with a sleek design.   Electronics            Electronics           \n",
       " \u001b[2m \u001b[0m\u001b[2mPrediction is correct.\u001b[0m\u001b[2m \u001b[0m \u001b[2m \u001b[0m\u001b[2mLaptop stand for the kitchen.     \u001b[0m\u001b[2m \u001b[0m \u001b[2m \u001b[0m\u001b[2mFurniture/Home Decor\u001b[0m\u001b[2m \u001b[0m \u001b[2m \u001b[0m\u001b[2mFurniture/Home Decor\u001b[0m\u001b[2m \u001b[0m \n",
       "  Prediction is correct.   Chocolate leather boots.             Footwear/Clothing      Footwear/Clothing     \n",
       " \u001b[2m \u001b[0m\u001b[2mPrediction is correct.\u001b[0m\u001b[2m \u001b[0m \u001b[2m \u001b[0m\u001b[2mWooden cream for surfaces.        \u001b[0m\u001b[2m \u001b[0m \u001b[2m \u001b[0m\u001b[2mFurniture/Home Decor\u001b[0m\u001b[2m \u001b[0m \u001b[2m \u001b[0m\u001b[2mFurniture/Home Decor\u001b[0m\u001b[2m \u001b[0m \n",
       "  Prediction is correct.   Natural finish for your lips.        Beauty/Personal Care   Beauty/Personal Care  \n",
       "                                                                                                             \n"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/html": [
       "<pre style=\"white-space:pre;overflow-x:auto;line-height:normal;font-family:Menlo,'DejaVu Sans Mono',consolas,'Courier New',monospace\">No errors found!\n",
       "</pre>\n"
      ],
      "text/plain": [
       "No errors found!\n"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/html": [
       "<pre style=\"white-space:pre;overflow-x:auto;line-height:normal;font-family:Menlo,'DejaVu Sans Mono',consolas,'Courier New',monospace\">Train is done!\n",
       "</pre>\n"
      ],
      "text/plain": [
       "Train is done!\n"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "from adala.agents import Agent\n",
    "from adala.environments import StaticEnvironment\n",
    "from adala.skills import ClassificationSkill\n",
    "\n",
    "agent = Agent(\n",
    "    skills=ClassificationSkill(\n",
    "        name='product_category_classification',\n",
    "        input_template='Text: {text}',\n",
    "        output_template='Category: {predicted_category}',\n",
    "        labels={'predicted_category':[\n",
    "            \"Footwear/Clothing\",\n",
    "            \"Electronics\",\n",
    "            \"Food/Beverages\",\n",
    "            \"Furniture/Home Decor\",\n",
    "            \"Beauty/Personal Care\"\n",
    "        ]},\n",
    "    ),\n",
    "    environment=StaticEnvironment(\n",
    "        df=df,\n",
    "        ground_truth_columns={'predicted_category': 'category'}\n",
    "    )\n",
    ")\n",
    "\n",
    "agent.learn()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<pre style=\"white-space:pre;overflow-x:auto;line-height:normal;font-family:Menlo,'DejaVu Sans Mono',consolas,'Courier New',monospace\"><span style=\"color: #000080; text-decoration-color: #000080; font-weight: bold\">Total Agent Skills: </span><span style=\"color: #000080; text-decoration-color: #000080; font-weight: bold\">1</span>\n",
       "\n",
       "<span style=\"color: #008000; text-decoration-color: #008000; font-weight: bold; text-decoration: underline\">product_category_classification</span>\n",
       "<span style=\"color: #008000; text-decoration-color: #008000\">Label the input text with the appropriate category from the following options: </span><span style=\"color: #008000; text-decoration-color: #008000; font-weight: bold\">{</span><span style=\"color: #008000; text-decoration-color: #008000\">categories</span><span style=\"color: #008000; text-decoration-color: #008000; font-weight: bold\">}</span><span style=\"color: #008000; text-decoration-color: #008000\">. Ensure that the </span>\n",
       "<span style=\"color: #008000; text-decoration-color: #008000\">predicted category accurately reflects the content of the input text, as indicated by the user feedback.</span>\n",
       "\n",
       "</pre>\n"
      ],
      "text/plain": [
       "\u001b[1;34mTotal Agent Skills: \u001b[0m\u001b[1;34m1\u001b[0m\n",
       "\n",
       "\u001b[1;4;32mproduct_category_classification\u001b[0m\n",
       "\u001b[32mLabel the input text with the appropriate category from the following options: \u001b[0m\u001b[1;32m{\u001b[0m\u001b[32mcategories\u001b[0m\u001b[1;32m}\u001b[0m\u001b[32m. Ensure that the \u001b[0m\n",
       "\u001b[32mpredicted category accurately reflects the content of the input text, as indicated by the user feedback.\u001b[0m\n",
       "\n"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "from rich import print\n",
    "\n",
    "print(agent.skills)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<pre style=\"white-space:pre;overflow-x:auto;line-height:normal;font-family:Menlo,'DejaVu Sans Mono',consolas,'Courier New',monospace\">Applying skill: product_category_classification\n",
       "</pre>\n"
      ],
      "text/plain": [
       "Applying skill: product_category_classification\n"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "100%|█| 5/5 [00:01<00:00, \n"
     ]
    }
   ],
   "source": [
    "test_df = pd.DataFrame([\n",
    "    \"Stainless steel apple peeler.\",  # Potential categories: Electronics or Food/Beverages\n",
    "    \"Silk finish touch screen.\",      # Potential categories: Electronics or Beauty/Personal Care\n",
    "    \"Chocolate coated boots.\",        # Potential categories: Footwear/Clothing or Food/Beverages\n",
    "    \"Natural wood fragrance.\",        # Potential categories: Beauty/Personal Care or Furniture/Home Decor\n",
    "    \"Leather grain snack bar.\"        # Potential categories: Footwear/Clothing or Food/Beverages\n",
    "], columns=['text'])\n",
    "\n",
    "predictions = agent.run(test_df)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>text</th>\n",
       "      <th>predicted_category</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>Stainless steel apple peeler.</td>\n",
       "      <td>Furniture/Home Decor</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>Silk finish touch screen.</td>\n",
       "      <td>Electronics</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>Chocolate coated boots.</td>\n",
       "      <td>Food/Beverages</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>Natural wood fragrance.</td>\n",
       "      <td>Beauty/Personal Care</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>Leather grain snack bar.</td>\n",
       "      <td>Food/Beverages</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                            text    predicted_category\n",
       "0  Stainless steel apple peeler.  Furniture/Home Decor\n",
       "1      Silk finish touch screen.           Electronics\n",
       "2        Chocolate coated boots.        Food/Beverages\n",
       "3        Natural wood fragrance.  Beauty/Personal Care\n",
       "4       Leather grain snack bar.        Food/Beverages"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "predictions"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "adala",
   "language": "python",
   "name": "adala"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
